package com.jilujia.domain.question.repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.jilujia.domain.question.Question;
import com.jilujia.framework.domain.JpaBusinessRepository;

@Repository
@Transactional(readOnly = true)
public interface QuestionRepository extends JpaBusinessRepository<Long, Question> {

	@Query("select a from Question a, QuestionTagRelation b, QuestionTag c where a.id = b.questionId and b.tagId=c.id and c.name = :tagName")
	Page<Question> findQuestionsByTag(@Param("tagName") String tagName, Pageable pageable);

	@Query("select a from Question a, QuestionTagRelation b, QuestionTag c where a.id = b.questionId and b.tagId=c.id and c.regionId = :regionId")
	List<Question> findQuestionsByRegion(@Param("regionId") Long regionId, Pageable pageable);

	@Query("select a.viewCount from Question a where a.id = :questionId")
	Integer findQuestionViewCountByQuestion(@Param("questionId") Long questionId);

	@Transactional
	@Modifying
	@Query("update Question a set a.viewCount=:viewCount where a.id = :questionId")
	void saveViewCount(@Param("questionId") Long questionId, @Param("viewCount") Integer viewCount);
}
